﻿namespace Vibstudio.NetConfigurator.UI.Converters
{
    using System;
    using System.Globalization;
    using System.Linq;
    using System.Windows.Data;
    using System.Windows.Media;
    using System.Windows.Media.Effects;

    using Vibstudio.NetConfigurator.Globalization;
    using Vibstudio.NetConfigurator.UI.Model;

    [ValueConversion(typeof(bool), typeof(BitmapEffect))]
    public class ResultConverter : IValueConverter
    {
        class ResultDetail
        {
            public int Count { get; set; }
            public int Failures { get; set; }
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                ResultDetail detail = GetResultDetail(value);
                int results = detail.Count;
                int failures = detail.Failures;

                if (failures <= 0)
                {
                    if (Equals(parameter, "BitmapEffect"))
                        return new OuterGlowBitmapEffect {GlowSize = 8, GlowColor = Colors.Green};
                    if (Equals(parameter, "ConfigurationResultMessageContent"))
                        return Labeler.Get.SuccessfulConfiguration;
                    if (Equals(parameter, "CompositionResultMessageContent"))
                        return Labeler.Get.SuccessfulComposition;
                    if (Equals(parameter, "ValidationResultMessageContent"))
                        return Labeler.Get.SuccessfulValidation;
                    if (Equals(parameter, "ResultMessageForeground"))
                        return new BrushConverter().ConvertFromString("#007F0E") as Brush;
                }

                if (failures >= 1 && failures < results)
                {
                    if (Equals(parameter, "BitmapEffect"))
                        return new OuterGlowBitmapEffect { GlowSize = 8, GlowColor = Colors.Orange };
                    if (Equals(parameter, "ConfigurationResultMessageContent"))
                        return Labeler.Get.PartialConfiguration(GetSuccessPercentage(results, failures));
                    if (Equals(parameter, "CompositionResultMessageContent"))
                        return Labeler.Get.PartialComposition(GetSuccessPercentage(results, failures));
                    if (Equals(parameter, "ValidationResultMessageContent"))
                        return Labeler.Get.PartialValidaton(GetSuccessPercentage(results, failures));
                    if (Equals(parameter, "ResultMessageForeground"))
                        return new BrushConverter().ConvertFromString("#FF6A00") as Brush;
                }

                if (failures >= results)
                {
                    if (Equals(parameter, "BitmapEffect"))
                        return new OuterGlowBitmapEffect { GlowSize = 8, GlowColor = Colors.Red };
                    if (Equals(parameter, "ConfigurationResultMessageContent"))
                        return Labeler.Get.ConfigurationNotPerformed;
                    if (Equals(parameter, "CompositionResultMessageContent"))
                        return Labeler.Get.CompositionNotPerformed;
                    if (Equals(parameter, "ValidationResultMessageContent"))
                        return Labeler.Get.ValidationNotPerformed;
                    if (Equals(parameter, "ResultMessageForeground"))
                        return new BrushConverter().ConvertFromString("#FF0000") as Brush;
                }
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }

        private ResultDetail GetResultDetail(object value)
        {
            switch (value.GetType().Name)
            {
                case "ResultConfigurationCollection":
                    var resultConfiguration = (ResultConfigurationCollection)value;
                    return new ResultDetail
                    {
                        Count = resultConfiguration.Count,
                        Failures = resultConfiguration.Count(r => r.IsValid == false)
                    };
                case "ResultValidationUrlCollection":
                    var resultValidationUrl = (ResultValidationUrlCollection)value;
                    return new ResultDetail
                    {
                        Count = resultValidationUrl.Count,
                        Failures = resultValidationUrl.Count(r => r.IsValid == false)
                    };
                case "ResultValidationFileCollection":
                    var resultValidationFile = (ResultValidationFileCollection)value;
                    return new ResultDetail
                    {
                        Count = resultValidationFile.Count,
                        Failures = resultValidationFile.Count(r => r.IsValid == false)
                    };
                default:
                    return new ResultDetail
                    {
                        Count = 0,
                        Failures = 0
                    };
            }
        }

        private int GetSuccessPercentage(int results, int failures)
        {
            return (((results - failures) * 100) / results);
        }
    }
}